home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr34 / blowpas.zip / BLOWF_F.ASM < prev    next >
Assembly Source File  |  1994-12-30  |  3KB  |  114 lines

  1. ; Assembly implementation of BlowFish F function.
  2. ;
  3. ; Original algoritm by Bruce Schneier
  4. ; First assembly implementation by John Lots and Walter van Holst
  5. ; Assembly rewrite by Jeroen Pluimers
  6. ;
  7. ; This code is hereby donated to the public domain
  8. ;
  9. ; Version history of rewritten code:
  10. ;   19941230 1.00.00 jwp - implemented assembly code from scratch
  11. ;
  12. ; Original Pascal code:
  13. ;   Type TSBox = array[1..4,0..255] of Longint;
  14. ;   function F(Input: Longint; var SBox: TSBox): Longint; near;
  15. ;   var
  16. ;     Bytes: array[0..3] of Byte absolute Input;
  17. ;   begin
  18. ;     F:=(
  19. ;          ( SBox[1,Bytes[0]] +
  20. ;            SBox[2,Bytes[1]]
  21. ;          ) XOR
  22. ;          SBox[3,Bytes[2]]
  23. ;        ) +
  24. ;        SBox[4,Bytes[3]];
  25. ;   end;
  26.  
  27. Ideal
  28. P386
  29. Model   TPascal
  30.  
  31. CodeSeg
  32.  
  33.  
  34.   Struc TSBox
  35.     box1          dd      256 dup(?)
  36.     box2          dd      256 dup(?)
  37.     box3          dd      256 dup(?)
  38.     box4          dd      256 dup(?)
  39.   EndS
  40.  
  41.   Proc    F Near Input: DWord, SBox: DWord
  42.   Public  F
  43.   ; NOTE:
  44.   ;  - there is no RETURNS keyword as this is Pascal calling convention
  45.   ;  - Result is returned in DX:AX
  46.   ;  - This routine is NEAR and can only be called locally from within a unit
  47.  
  48.   ; stack frame is automatically generated
  49.  
  50.           push    ds
  51.  
  52.           ; load the passed data
  53.           lds     si,  [SBox]
  54.           mov     edx, [Input]
  55.           mov     di,  si  ; save si for later use
  56.  
  57.           ; calculate A, B, C and D in ax, bx, cx, dx
  58.  
  59.           xor     ax,  ax
  60.           xor     bx,  bx
  61.           xor     cx,  cx
  62.  
  63.           mov     al,  dl
  64.           mov     bl,  dh
  65.  
  66.           shr     edx, 16  ; get high word of edx into dx
  67.           mov     cl,  dl
  68.  
  69.           shr     dx,  8   ; get high byte of dx into dl and clear dh
  70.           ; the code above is faster than below:
  71.           ;   mov      dl, dh
  72.           ;   xor      dh, dh  ; maak dh leeg
  73.  
  74.           ; adjust A, B, C, D as offsets into SBox
  75.           shl     ax,  2
  76.           shl     bx,  2
  77.           shl     cx,  2
  78.           shl     dx,  2
  79.           add     ax,  si
  80.           add     bx,  di
  81.           add     cx,  si
  82.           add     dx,  di
  83.  
  84.           ; apply the F algoritm
  85.  
  86.           mov     si,  ax
  87.           mov     eax, [DWord ptr ds:si]
  88.           ; originally [... + TSBox.box1] but by definition TSBox.box1 == 0
  89.  
  90.           mov     si,  bx
  91.           add     eax, [DWord ptr ds:si + TSBox.box2]
  92.  
  93.           mov     si,  cx
  94.           xor     eax, [DWord ptr ds:si + TSBox.box3]
  95.  
  96.           mov     si,  dx
  97.           add     eax, [DWord ptr ds:si + TSBox.box4]
  98.  
  99.           ; get high 16 bits of eax into dx
  100.           mov     edx, eax
  101.           shr     edx, 16
  102.  
  103.           pop     ds
  104.  
  105.           ret
  106.  
  107.   ; stack cleanup is automatically generated
  108.  
  109.   EndP ; F
  110.  
  111. EndS  ; CodeSeg
  112.  
  113. End   ; Source
  114.